package sbt.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import sbt.model.Film;

public class InternalPage extends AnyPage {

	public InternalPage(PageManager pages) {
		super(pages);
	}
	
  public InternalPage ensurePageLoaded() {
    super.ensurePageLoaded();
    wait.until(presenceOfElementLocated(By.cssSelector("nav")));
    return this;
  }
  
  @FindBy(css = "nav a[href $= '?go=home']")
  private WebElement homeLink;
 
  @FindBy(css = "nav a[href $= '?go=profile']")
  private WebElement userProfileLink;

  @FindBy(css = "nav a[href $= '?go=add']")
  private WebElement addFilmLink;

  @FindBy(css = "nav a[href $= '?go=users']")
  private WebElement userManagementLink;

  @FindBy(css = ".center > nav > ul > li > a[href *='logout']")
  private WebElement logoutLink;
  
 @FindBy (xpath = "//div[@id='search']//div[@class='searchbox']/input[@id='q']")
 private WebElement searchBox;
  
  public AddFilmPage clickAddFilmLink() {
	  addFilmLink.click();
	  return pages.addfilmPage;
  } 
  public UserProfilePage clickUserProfilePage() {
    userProfileLink.click();
    return pages.userProfilePage;
  }

  public UserManagementPage clickUserManagementLink() {
    userManagementLink.click();
    return pages.userManagementPage;
  }
  
  public LoginPage clickLogoutLink() {
    logoutLink.click();
    wait.until(alertIsPresent()).accept();
    return pages.loginPage;
  }
  
  public boolean gotoFilmPage(Film film) {
	  WebElement clickedFilm = driver.findElement(By.xpath("//a [@href='./?go=movie&id="+film.getId()+"']"));
	  clickedFilm.click();
	  return pages.filmPage.waitPageLoaded();
  }
  
  public void clearSearch() throws Exception {
		searchBox.click();
		searchBox.sendKeys(Keys.CONTROL+"a");
		Thread.sleep(50);
		searchBox.sendKeys(Keys.DELETE);
		searchBox.sendKeys(Keys.RETURN);
		
  }
  
  public void setSearchText(String text) {
	  searchBox.click();
	  searchBox.sendKeys(text);
	  searchBox.sendKeys(Keys.RETURN);
  }
  

}
